using System;
using System.Reflection;

using SBPweb.Data;

namespace SBPweb.PROvibe.Resources.Entities
{
	public class Module: Entity, IModule
	{

		#region Private fields

		private const string FIELDNAME_Id = "Id";
		private const string FIELDNAME_Name = "Name";
		private const string FIELDNAME_AssemblyStreamId = "AssemblyStreamId";
		private const string FIELDNAME_AssemblyName = "AssemblyName";
		private const string FIELDNAME_AssemblyVersion = "AssemblyVersion";
		private const string FIELDNAME_AssemblyFileVersion = "AssemblyFileVersion";

		private AssemblyName mAsmName;
		private System.Version mAsmVersion;
		private System.Version mAsmFileVersion;

		#endregion

		#region Constructor logic

		static Module()
		{
			RegisterEntityTypeToColumns(typeof(Module));
		}

		public Module()
            : base(null)
		{
		}

        public Module(Module entity)
            : base(entity)
        {
        }

		#endregion

		#region Public property accessors

		public virtual int Id
		{
			get
			{
				return GetValue<Int32>(FIELDNAME_Id);
			}
			set
			{
				SetValue<Int32>(FIELDNAME_Id, value);
			}
		}

		public virtual string Name
		{
			get
			{
				return GetValue<String>(FIELDNAME_Name);
			}
			set
			{
				SetValue<String>(FIELDNAME_Name, value);
			}
		}

		public virtual int AssemblyStreamId
		{
			get
			{
				return GetValue<int>(FIELDNAME_AssemblyStreamId);
			}
			set
			{
				SetValue<int>(FIELDNAME_AssemblyStreamId, value);
			}
		}

		public virtual AssemblyName AssemblyName
		{
			get
			{
				if (mAsmName == null)
				{
					mAsmName = new AssemblyName(GetValue<String>(FIELDNAME_AssemblyName));
				}
				return mAsmName;
			}
			set
			{
				mAsmName = value;
				SetValue<String>(FIELDNAME_AssemblyName, value.FullName);
			}
		}

		public virtual System.Version AssemblyVersion
		{
			get
			{
				if (mAsmVersion == null)
				{
					mAsmVersion = new System.Version(GetValue<String>(FIELDNAME_AssemblyVersion));
				}
				return mAsmVersion;
			}
			set
			{
				mAsmVersion = value;
				SetValue<String>(FIELDNAME_AssemblyVersion, value.ToString());
			}
		}

		public virtual System.Version AssemblyFileVersion
		{
			get
			{
				if (mAsmFileVersion == null)
				{
					mAsmFileVersion = new System.Version(GetValue<String>(FIELDNAME_AssemblyFileVersion));
				}
				return mAsmFileVersion;
			}
			set
			{
				mAsmFileVersion = value;
				SetValue<String>(FIELDNAME_AssemblyFileVersion, value.ToString());
			}
		}

		#endregion

		#region Overridden methods

		public override object Clone()
		{
			throw new Exception("The method or operation is not implemented.");
		}

        public int CompareTo(IEntity other)
        {
            IModule o = other as IModule;
            if (o == null)
            {
                return 0;
            }
            return this.Id.CompareTo(o.Id);
        }

        public bool Equals(IEntity other)
        {
            IModule o = other as IModule;
            if (o == null)
            {
                return false;
            }
            return this.Id.Equals(o.Id);
        }

        #endregion
    }
}
